import pymysql


# 修改
def upda():
    # 连接数据库
    con = pymysql.connect(host='192.168.1.240', port=3306, user='root', password='123456', db='pnp_test_only',
                          charset='utf8')
    try:
        # 开启游标功能，创建游标对象并重命名
        with con.cursor() as cur:
            # 执行SQL语句,语句中的参数用%s占位，再传参数
            res = cur.execute('UPDATE assistant_version SET ver=%s WHERE barid=%s', ('5.6', '100125'));
            if res == 1:
                print('update success')
            # 提交事务，update,delete,insert等修改表中数据的需要commit，create,drop,alter等修改表结构的,就不需要commit,因为内部隐藏了commit
            con.commit()
    except pymysql.MySQLError as error:
        print(error)
        # 运行失败就回滚
        con.rollback()
    finally:
        # 断开数据库，释放资源
        con.close()


# 删除
def dele():
    # 连接数据库
    con = pymysql.connect(host='192.168.1.240', port=3306, user='root', password='123456', db='pnp_test_only',
                          charset='utf8')
    try:
        # 开启游标功能，创建游标对象并重命名
        with con.cursor() as cur:
            # 执行SQL语句,语句中的参数用%s占位，当参数只有一个时需要添加逗号
            res = cur.execute('DELETE FROM assistant_version WHERE barid=%s', ('100125',));
            if res == 1:
                print('delete success')
            # 提交事务，update,delete,insert等修改表中数据的需要commit，create,drop,alter等修改表结构的,就不需要commit,因为内部隐藏了commit
            con.commit()
    except pymysql.MySQLError as error:
        print(error)
        # 运行失败就回滚
        con.rollback()
    finally:
        # 断开数据库，释放资源
        con.close()


if __name__ == '__main__':
    upda()
    dele()
